/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.util;

import junit.framework.TestCase;

/**
 * @author caliendo
 */
public class ConcurrentMethodCacheTest extends TestCase {

  /** Create a simple cache which will just count the number of times the method is invoked */
  private static final ConcurrentMethodCache<Integer, Integer> cache = new ConcurrentMethodCache<Integer, Integer>(
      new ConcurrentMethodCache.Method<Integer, Integer>() {
        private volatile int invokeCount = 0;
        public Integer invoke(Integer param) {
          return ++invokeCount;
        }
      }  
    );
  
  private static void assertNotEquals(final String message, final Object one, final Object two) {
    assertFalse(message + ", expected NOT [" + one + "] got [" + two + "]",one.equals(two));
  }
  // -----
  // tests
  // -----
  
  public void testGetCachesResults() throws Exception {
    final Integer valueFor0 = cache.get(0);
    final Integer valueFor1 = cache.get(1);
    
    assertEquals("expected first invocation to return 1 invocation count [0]", valueFor0, cache.get(0));
    assertEquals("expected second invocation to return 1 invocation count [0]", valueFor0, cache.get(0));
    
    assertEquals("expected first invocation to return 1 invocation count [1]", valueFor1, cache.get(1));
    assertEquals("expected second invocation to return 1 invocation count [1]", valueFor1, cache.get(1));
    assertEquals("expected third invocation to return 1 invocation count [1]", valueFor1, cache.get(1));
  }
  
  public void testClearClearsCache() throws Exception {
    final Integer valueFor0 = cache.get(0);
    final Integer valueFor1 = cache.get(1);
    
    cache.clear();
    
    assertNotEquals("expected the cached results to be cleared (method should re-invoke, increasing the invokeCount)", valueFor0, cache.get(0));
    assertNotEquals("expected the cached results to be cleared (method should re-invoke, increasing the invokeCount)", valueFor1, cache.get(1));
  }
  
  public void testRemoveRemovesOnlySpecified() throws Exception {
    final Integer valueFor0 = cache.get(0);
    final Integer valueFor1 = cache.get(1);
    
    cache.remove(0);
    
    assertNotEquals("expected removed param to not return the cached result", valueFor0, cache.get(0));
    assertEquals("expected non-removed param to return cached result", valueFor1, cache.get(1));
  }

}
